package com.developtools.doc;

import com.developtools.model.ClassApiInfo;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author liujun
 */
public class DocumentManager {

    private static final Map<String, Class<? extends DocumentGenerate>> documentMap = new ConcurrentHashMap<>();

    static {
        documentMap.put("markdown", MarkdownGenerate.class);
    }

    private static DocumentManager documentManager;

    private DocumentManager(){}

    public static DocumentManager getInstance(){
        if (Objects.isNull(documentManager)) {
            documentManager = new DocumentManager();
        }
        return documentManager;
    }

    public void build(String documentType,File file, List<ClassApiInfo> contentList){
        Class<? extends DocumentGenerate> clazz = documentMap.get(documentType);
        DocumentGenerate documentGenerate = null;
        try {
            documentGenerate = clazz.getConstructor().newInstance();
            documentGenerate.generate(file, contentList);
        } catch (InstantiationException | IllegalAccessException | InvocationTargetException
                 | NoSuchMethodException e) {
            throw new RuntimeException(e);
        }
    }
}
